//
//  CustomTripCell.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "CustomTripCell.h"
#import "TripModel.h"
#import "UIImageView+WebCache.h"

@implementation CustomTripCell


- (void)dealloc
{
    [_model release];
    [_dayLabel release];
    [_placeLabel release];
    [_countLabel release];
    [_addressLabel release];
    [_timeLabel release];
    [_nameLabel release];
    [_image release];
    //[_titleButton release];
    [self removeObserver:self forKeyPath:@"model" context:nil];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.image = [[UIImageView alloc] initWithFrame:CGRectZero];
        [_image setClipsToBounds:YES];
        [_image.layer setCornerRadius:10];
        //[_image setBackgroundColor:[UIColor redColor]];
        [self.contentView addSubview:_image];
        [_image release];
        
        
        self.addressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
       // [_addressLabel setBackgroundColor:[UIColor cyanColor]];
        [self.contentView addSubview:_addressLabel];
        [_addressLabel release];
        
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        //[_timeLabel setBackgroundColor:[UIColor cyanColor]];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        //[_nameLabel setBackgroundColor:[UIColor cyanColor]];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel release];
        
        
        
        
        
        self.dayLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_dayLabel setBackgroundColor:[UIColor cyanColor]];
        //[_dayLabel setText:@"天"];
        [self.contentView addSubview:_dayLabel];
        [_dayLabel release];
        
        self.countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_countLabel setBackgroundColor:[UIColor cyanColor]];
        [self.contentView addSubview:_countLabel];
        [_countLabel release];
        
        
        self.placeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_placeLabel setBackgroundColor:[UIColor cyanColor]];
        [self.contentView addSubview:_placeLabel];
        [_placeLabel release];
        
        
        [self addObserver:self forKeyPath:@"model" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"model"]) {
        [self.nameLabel setText:self.model.name];
        [self.timeLabel setText:self.model.first_day];
        [self.placeLabel setText:self.model.popular_place_str];
        NSNumber *number = [NSNumber numberWithInteger:_model.day_count];
        [self.dayLabel setText:[NSString stringWithFormat:@"%@", number]];
        NSNumber *number1 = [NSNumber numberWithInteger:_model.view_count];
        [self.countLabel setText:[NSString stringWithFormat:@"%@", number1]];
        [self.image setImageWithURL:[NSURL URLWithString:_model.cover_image]];
    }
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    //[_titleButton setFrame:CGRectMake(10, 10, self.frame.size.width - 20, 30)];
    
    
    [_image setFrame:CGRectMake(10, 10, self.frame.size.width - 20, 188)];
    
    
    [_nameLabel setFrame:CGRectMake(_image.frame.origin.x + 10, _image.frame.origin.y + 10, 130, 20)];
    
    
    [_timeLabel setFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 5, 100, 20)];
    
    
    [_dayLabel setFrame:CGRectMake(_timeLabel.frame.origin.x + _timeLabel.frame.size.width + 5, _timeLabel.frame.origin.y, 10, _timeLabel.frame.size.height)];
    
    
    [_countLabel setFrame:CGRectMake(_dayLabel.frame.origin.x + _dayLabel.frame.size.width + 5, _timeLabel.frame.origin.y, 50, _timeLabel.frame.size.height)];
    
    
    [_placeLabel setFrame:CGRectMake(_timeLabel.frame.origin.x, _timeLabel.frame.origin.y + _timeLabel.frame.size.height + 5, 50, 20)];
    
    
    
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
